/*
 * $Header: $
 */
package net.frankish.moviebank.back;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import net.frankish.moviebank.back.db.DerbyDatabase;
import net.frankish.moviebank.beans.Movie;

/**
 *
 * <p>Title: $RCSfile: PersistenceManager.java,v $</p>
 * File: PersistenceManager.java
 * Project: JCharacter
 * @author Frankish
 * @version $Revision:  $
 * @param <T>
 */
public class PersistenceManager{

	private HashMap beans = new HashMap();
	private XMLController controller = new XMLController();
	private static PersistenceManager instance = null;

	private PersistenceManager(){
		loadData();
	}

	public HashMap<Class,HashMap> loadData(){
		this.beans = new HashMap<Class,HashMap>();
		ArrayList array = new ArrayList(this.controller.loadData());
		Iterator it = array.iterator();
		while(it.hasNext()){

			Object obj = it.next();
			Class name = obj.getClass();
			PersistentBean bean = (PersistentBean)obj;
			if(!(obj instanceof PersistentBean))
				continue;

			HashMap list = (HashMap)beans.get(name);
			if(list == null){
				list = new HashMap();
			}
			list.put(bean.getKey(),bean);
			beans.put(name, list);
		}
		return beans;
	}

	public static PersistenceManager getInstance(){
		if(instance == null){
			instance = new PersistenceManager();
		}
		return instance;
	}

	public PersistentBean findByKey(Class type, Long id){
		if(id == null)
			return null;
		HashMap bean = (HashMap)beans.get(type);
		if(bean == null)
			return null;
		return (PersistentBean)((PersistentBean)bean.get(id));
	}

	public Vector<Movie> findAll(Class type){
		if(type.equals(Movie.class)){
			return DerbyDatabase.findAllMovie();
		}
		return new Vector();
	}

	public void store(PersistentBean bean) throws Exception{
		if(bean instanceof Movie){
			try {
				if(((Movie)bean).getId() == 0)
					DerbyDatabase.insertMovie((Movie)bean);
				else
					DerbyDatabase.updateMovie((Movie)bean);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void store(Collection beans) throws Exception{
		Iterator it = beans.iterator();
		while(it.hasNext())
			store((PersistentBean)it.next());
	}

	private void create(PersistentBean bean) throws Exception{
//		controller.create(bean.getKey()+".xml", bean);
//		refresh(bean);
	}

	private void update(PersistentBean bean) throws Exception{
//		controller.update(bean.getKey()+".xml", bean);
//		refresh(bean);
	}

	public void remove(PersistentBean bean){
		controller.remove(bean.getKey()+".xml");
		Class name = bean.getClass();
		HashMap list = (HashMap)beans.get(name);
		if(list == null)
			return;
		list.remove(bean.getKey());
		beans.put(name, list);
	}

//	public void refresh(PersistentBean bean) throws Exception{
//		PersistentBean res = (PersistentBean)controller.refresh(bean.getKey()+".xml");
//		Class name = res.getClass();
//		HashMap list = (HashMap)beans.get(name);
//		if(list == null){
//			list = new HashMap();
//		}
//		list.put(bean.getKey(),bean);
//		beans.put(name, list);
//	}

}
